package club.itstudio.itouc.util

import java.math.BigInteger
import java.nio.charset.StandardCharsets

/**
 * 图书馆登录工具
 */

object Library {

    private fun getToken(var0: Long): String {
        return if (var0 < 10L) {
            var0.toString()
        } else {
            if (var0 < 36L) (65L + (var0 - 10L)).toInt().toChar().toString() else ""
        }
    }

    fun getToken(var0: String): String {
        var var00 = var0
        var00 += ",中国海洋大学"
        val var2: Byte = 0
        var var1 = 0

        val var4 = var00.toByteArray(StandardCharsets.UTF_8)
        var00 = ""

        while (true) {
            if (var1 >= var4.size) {
                var00 = var00.toUpperCase()
                break
            }
            val var5 = Integer.toHexString(var4[var1].toInt().and(255))
            var00 += when(var5.length == 1) {
                true ->  "0$var5"
                false ->  var5
            }
            ++var1
        }

        val var11 = StringBuilder()

        var1 = 0
        while (var1 < var00.length) {
            var11.append(var00[var1])
            if (var1 % 2 == 0 && var1 != 0 && var1 < 12) {
                var11.append((Math.random() * 10.0).toInt())
            }
            ++var1
        }

        var00 = b(var11.toString())
        val var3: Byte = 0
        var1 = var2.toInt()

        var var10: Int
        var10 = var3.toInt()
        while (var1 < var00.length) {
            var10 += Integer.parseInt("" + var00[var1])
            ++var1
        }

        var10 %= 36
        if (var10 > 10) {
            var1 = var10
            if (var10 != 16) {
                return getToken(var00, var1) + getToken(var1.toLong())
            }
        }

        var1 = 18
        return getToken(var00, var1) + getToken(var1.toLong())
    }

    private fun getToken(var0: String, var1: Int): String {
        var var00 = var0
        var var11 = var1
        val var4 = arrayOfNulls<BigInteger>(500)

        var var2  = 0
        while (var2 < 100) {
            var4[var2] = BigInteger("0")
            ++var2
        }

        var var6 = BigInteger(var00)
        val var5 = BigInteger(var11.toString())

        var11 = 0
        while (var6.toString().length != 1 || var6.toInt() != 0) {
            var4[var11] = var6.mod(var5)
            var6 = var6.subtract(var4[var11]).divide(var5)
            ++var11
        }

        var2 = var11 - 1

        var var3 = 0
        while (var3 < var11 / 2) {
            var6 = var4[var3]!!
            var4[var3] = var4[var2]
            var4[var2] = var6
            ++var3
            --var2
        }

        var2 = 0

        var00 = ""
        while (var2 < var11) {
            var00 += getToken(java.lang.Long.parseLong(var4[var2].toString()))
            ++var2
        }

        return var00
    }

    private fun b(var0: String): String {
        val var5 = BigInteger(16.toString())
        var var4 = BigInteger("0")
        BigInteger("0")

        for (var3 in 0 until var0.length) {
            val var1 = var0[var3]
            val var2: Int

            var2 = when (var1) {
                in '0'..'9' -> Integer.parseInt(var1.toString())
                in 'A'..'Z' -> var1.toInt() - 65 + 10
                else -> -1
            }
            var4 = var4.add(var5.pow(var0.length - var3 - 1).multiply(BigInteger(var2.toString())))
        }

        return var4.toString()
    }
}

